package six;

import java.util.Arrays;

public class 六四Arrays类 {
    public static void main(String[] args) {
        int[] c = {1, 2};
        System.out.println(c); // 打印数组引用的值；
        System.out.println(Arrays.toString(c)); // 打印数组元素的值；

        int[] b = {1, 2, 323, 23, 543, 12, 59};
        System.out.println(Arrays.toString(b));
        Arrays.sort(b);
        System.out.println(Arrays.toString(b));

        Man[] msMans = {new Man(3, "a"), new Man(60, "b"), new Man(2, "c")};
        Arrays.sort(msMans);
        System.out.println(Arrays.toString(msMans));

        int[] a = {1, 2, 323, 23, 543, 12, 59};
        System.out.println(Arrays.toString(a));
        Arrays.sort(a);   //使用二分法查找，必须先对数组进行排序;
        System.out.println(Arrays.toString(a));
        //返回排序后新的索引位置,若未找到返回负数。
        System.out.println("该元素的索引：" + Arrays.binarySearch(a, 12));

        int[] d = {1, 2, 323, 23, 543, 12, 59};
        System.out.println(Arrays.toString(d));
        Arrays.fill(d, 2, 4, 100);  //将2到4索引的元素替换为100;
        System.out.println(Arrays.toString(d));
    }
}

class Man implements Comparable {
    int age;
    int id;
    String name;

    public Man(int age, String name) {
        super();
        this.age = age;
        this.name = name;
    }

    public String toString() {
        return this.name;
    }

    public int compareTo(Object o) {
        Man man = (Man) o;
        if (this.age < man.age) {
            return -1;
        }
        if (this.age > man.age) {
            return 1;
        }
        return 0;
    }
}
